rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)
library(ggplot2)

m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, age := 2021 - year]
m[, experience := experience / 12]

m[, retrecall_1day_rate := retrecall_1day_rate * 100]
m[, retrecall_30day_rate := retrecall_30day_rate * 100]
m[, retrecall_1year_rate := retrecall_1year_rate * 100]
m[, retrecall_5year_rate := retrecall_5year_rate * 100]

m[, expret30day_market_rate := expret30day_market_rate * 100]
m[, expret1year_market_rate := expret1year_market_rate * 100]
m[, expret30day_self_rate := expret30day_self_rate * 100]
m[, expret1year_self_rate := expret1year_self_rate * 100]

m[, memrableperiod_pret := memrableperiod_pret * 100]
m[, memrableperiod_aret := memrableperiod_aret * 100]

m[, memrableperiod_begin := as.yearmon(memrableperiod_begin, "%Ym%m")]
m[, memrableperiod_end := as.yearmon(memrableperiod_end, "%Ym%m")]

###########################################################################
# What period is recalled?
###########################################################################
m <- m[!is.na(experience)]

m[, start.trading := as.yearmon(2021 + 11 / 12) - experience]

yms <- seq(min(m$memrableperiod_begin), as.yearmon(2021 + 11 / 12), 1 / 12)
C <- data.table(ym = yms, count = 0)

for (i in 1:length(m$id))
{
  init.period <- m$memrableperiod_begin[i]
  end.period <- as.yearmon(2021 + 11 / 12)
  list <- seq(init.period, end.period, 1 / 12)

  C1 <- data.table(ym = list, count1 = 1 / length(list))
  C <- merge(C, C1, by = "ym", all.x = T)
  C[is.na(count1), count1 := 0]
  C[, count := count + count1]
  C[, count1 := NULL]
}

C[, count := count / length(m$id) * 100]

## merge in SH daily index
sh <- data.table(read_xlsx("../Data/SHIndex.xlsx", guess_max = 1e4))
sh <- sh[, .(Idxtrd01, Idxtrd05)]
names(sh) <- c("date", "SHindex")
sh <- sh[-1]
sh[, date := as.Date(date)]
setorder(sh, date)

sh[, ym := as.yearmon(date)]
sh <- sh[, .(SHindex = SHindex[.N]), by = .(ym)]
sh[, SHindex := as.numeric(SHindex)]
sh[, SHindex := SHindex]

C <- merge(C, sh, by = c("ym"), all.y = T)
C[, count := count * 1e3 * .75]

ggplot(C) +
  xlab("") +
  ylab("") +
  geom_bar(aes(x = ym, y = count), stat = "identity") +
  geom_line(aes(x = ym, y = SHindex), color = "blue") +
  theme_minimal() +
  ylim(c(0, 8e3)) +
  scale_x_continuous(expand = c(0, 0), limits = c(1992, 2022))
ggsave("../Figures/pic-recallcounterfactual-normalized.pdf", width = 6, height = 4)
